<?php

class Groups_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = 'set_user_groups';
        $this->tabs_tableName = 'set_tabs';
        $this->accType_tableName = 'set_access_types';
        $this->load->model("functions_model");
    }

    function newGroup($added_by) {
        $data = $_POST;
        $data['added_by'] = $added_by;
        $data['last_modified'] = date("Y-m-d H:i:s");
        $this->db->insert($this->tableName, $data);
    }

    function delete($tid) {
        $data['deleted'] = 1;
        $where['tid'] = $tid;
        $this->db->where($where);
        $this->db->update($this->tableName, $data);
    }

    function size() {
        return $this->db->count_all($this->tabs_tableName);
    }

    function permisions($tid, $offset, $per_page) {
        $this->db->select('tid,name');
        $this->db->order_by("tab_order", "asc");
        $query = $this->db->get($this->tabs_tableName, $per_page, $offset);

        $this->db->select('tid, name');
        $this->db->from($this->accType_tableName);
        $typeQuery = $this->db->get();
        $accessType = array();
//        $accessType[666] = "No access";
        foreach ($typeQuery->result_array() as $row) {
            $accessType[$row['tid']] = $row['name'];
        }

        $result = array();

        $count = $offset;
        foreach ($query->result_array() as $row) {
            $count++;
            $access = $this->checkAccess($tid, $row['tid']);

            foreach ($typeQuery->result_array() as $typeRow) {
                $checked = $access == $typeRow['tid'] ? "checked=checked" : "";
                $row[] = "<input type='radio' name='tab" . $row['tid'] . "' value='" . $typeRow['tid'] . "' id='tab" . $row['tid'] . "' onchange='changed(\"tab" . $row['tid'] . "\"," . $row['tid'] . ", $tid, $offset)' $checked";
            }
//            unset($row['tid']);
            $row['tid'] = $count;
            $result[] = $row;
        }
        return $result;
    }

    function checkAccess($groupId, $tabId) {
        $query = $this->db->query("SELECT access_type FROM set_access_matrix WHERE
            group_id=$groupId and
                tab_id=$tabId");
        if ($query->num_rows() > 0) {
            $row = $query->row_array();
            return $row['access_type'];
        } else {
            return 666; //no access
        }
    }

    function setAccess($groupId, $tabId, $access) {
        $this->db->query("DELETE FROM set_access_matrix WHERE
            group_id=$groupId and
                tab_id=$tabId");
        $this->db->query("INSERT INTO set_access_matrix SET
            group_id=$groupId,
                tab_id=$tabId, access_type=$access");
    }

    function getAll() {
        $this->db->select('tid,added_by,name,description,last_modified');
        $this->db->from($this->tableName);
        $this->db->where("deleted", 0);
        $this->db->order_by("tid", "desc");
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            unset($row['tid']);
            $added_by = $row['added_by'];
            $row['added_by'] = "<a href='javascript:loadUrl(\"#admin_result\",\"" . base_url() . "index.php?/admin_mis/applicantProfile/$added_by\")'>" . $row['added_by'] . "</a>";
            $row['last_modified'] = $this->functions_model->prettyDateTime($row['last_modified']);
            $row['links'] = "<a href='javascript:callUrl(\"" . base_url() . "index.php?/admin_settings/deleteGroup/$tid\",
            \"Are you sure to delete this group?\")'><img src='" . base_url() . "/images/delete.png' alt='Delete'/></a>
                |
<a href='javascript:accessMenu(\"groupPermissions/$tid\")'><img src='" . base_url() . "/images/more.png' alt='Permisions'/></a>";
            $result[] = $row;
        }
        return $result;
    }

}

?>
